package com.tg.paw.ums.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tg.paw.common.util.CommonPageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.tg.paw.ums.service.IAdminRoleRelationService;
import com.tg.paw.ums.entity.AdminRoleRelation;
import com.tg.paw.common.util.CommonResult;
import com.tg.paw.common.constant.AllEnum;
import com.tg.paw.common.util.NameUtil;
import com.tg.paw.common.util.QueryUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * 后台用户和角色关系表 前端控制器
 * </p>
 *
 * @author Rubble
 * @since 2020-10-22
 */
@RestController
@Slf4j
@Api(tags = "后台用户和角色关系表管理", value = "AdminRoleRelationController")
public class AdminRoleRelationController {
    @Autowired
    private IAdminRoleRelationService adminRoleRelationService;


    @ApiOperation("根据条件分页查询后台用户和角色关系表列表")
    @GetMapping(value = "/api/ums/adminRoleRelation")
    public CommonResult getAdminRoleRelationByPage(AdminRoleRelation entity,
                                    @RequestParam(value = "current", defaultValue = "1") Integer pageNo,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "sorter", defaultValue = "") String sorter,
                                    @RequestParam(value = "filter", defaultValue = "") String filter) {
        try {
            QueryWrapper<AdminRoleRelation> queryWrapper = new QueryWrapper<>(entity);
            QueryUtil.buildQuery(queryWrapper,filter,sorter,AdminRoleRelation.class);
            return CommonPageResult.pageResult(adminRoleRelationService.page(new Page<>(pageNo, pageSize), queryWrapper));
        } catch (Exception e) {
            log.error("根据条件查询所有后台用户和角色关系表：{}", e.getMessage(), e);
        }
        return CommonResult.failed();
    }

    @ApiOperation("保存后台用户和角色关系表表")
    @PostMapping(value = "/api/ums/adminRoleRelation")
    public CommonResult saveAdminRoleRelation(@RequestBody AdminRoleRelation entity) {
            try {
            if (adminRoleRelationService.save(entity)) {
                return CommonResult.success();
            }
            } catch (Exception e) {
                log.error("保存后台用户和角色关系表表：{}", e.getMessage(), e);
                return CommonResult.failed();
            }
            return CommonResult.failed();
    }

    @ApiOperation("更新后台用户和角色关系表")
    @PutMapping(value = "/api/ums/adminRoleRelation/{id}")
    public CommonResult updateAdminRoleRelation(@RequestBody AdminRoleRelation entity,@ApiParam("后台用户和角色关系表id") @PathVariable Long id) {

        try {
            entity.setId(id);
            if (adminRoleRelationService.updateById(entity)) {
                return CommonResult.success();
            }
        } catch (Exception e) {
            log.error("更新后台用户和角色关系表：{}", e.getMessage(), e);
            return CommonResult.failed();
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除后台用户和角色关系表")
    @DeleteMapping(value = "/api/ums/adminRoleRelation/{id}")
    public CommonResult deleteAdminRoleRelation(@ApiParam("后台用户和角色关系表id") @PathVariable Long id) {
        try {
            if (adminRoleRelationService.removeById(id)) {
                return CommonResult.success();
            }
        } catch (Exception e) {
            log.error("删除后台用户和角色关系表：{}", e.getMessage(), e);
            return CommonResult.failed();
        }
        return CommonResult.failed();
    }

    @ApiOperation("查询后台用户和角色关系表")
    @GetMapping(value = "/api/ums/adminRoleRelation/{id}")
    public CommonResult getAdminRoleRelationById(@ApiParam("后台用户和角色关系表id") @PathVariable Long id) {
        try {
            AdminRoleRelation coupon = adminRoleRelationService.getById(id);
            return CommonResult.success(coupon);
        } catch (Exception e) {
            log.error("查询后台用户和角色关系表：{}", e.getMessage(), e);
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "批量删除后台用户和角色关系表")
    @DeleteMapping(value = "/api/ums/adminRoleRelation")
    @ResponseBody
    public CommonResult deleteBatch(@RequestParam("ids") List<Long> ids) {
        boolean success = adminRoleRelationService.removeByIds(ids);
        return CommonResult.instance(success);
    }

}
